﻿/*    Copyright 2012 Robert Williams
    
This file is part of FindUninstallString.

FindUninstallString is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

FindUninstallString is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with FindUninstallString.  If not, see <http://www.gnu.org/licenses/>.
*/

using System;
using System.Linq;
using System.Windows.Forms;
using Microsoft.Win32;

namespace FindUninstallString
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            String productName = tbProgName.Text.ToString();
            String registryKey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
            this.lblKey.Text = @"HKLM\" + registryKey;
            this.lblStatus.ResetText();
            this.lblStatus.Text = "Searching";
            this.listView.Clear();
            this.listView.View = View.Details;
            this.listView.Columns.Add("Application Name", 200, HorizontalAlignment.Center);
            this.listView.Columns.Add("Uninstall String", -2, HorizontalAlignment.Left);

            try
            {
                using (Microsoft.Win32.RegistryKey key = Registry.LocalMachine.OpenSubKey(registryKey))
                {
                    (from a in key.GetSubKeyNames()
                     let r = key.OpenSubKey(a)
                     select new
                     {
                         Application = r.GetValue("DisplayName"),
                         RegUninstallString = r.GetValue("UninstallString"),      //example; MsiExec.exe /X{D84A070E-2A31-464A-9830-39FAB5761D62}
                         Ver = r.GetValue("DisplayVersion"),
                         IsWindowsInstaller = r.GetValue("WindowsInstaller")
                     }
                    )
                    .Where(c => c.Application != null)
                    .Where(c => c.RegUninstallString != null)                    
                    .ToList()
                    .FindAll(c => c.Application.ToString().ToLower().Contains(productName.ToLower()))
                    .ForEach(c => this.listView.Items.Add(c.Application.ToString()).SubItems.Add(c.RegUninstallString.ToString())
                    );
                }
            }

            catch (Exception ex)
            {
                lblStatus.Text = ex.ToString();
            }

            this.listView.AutoResizeColumn(1, ColumnHeaderAutoResizeStyle.ColumnContent);
            this.lblStatus.Text = "Search Complete";

        }

        private void toolStripMenuItem1_Click(object sender, EventArgs e)
        {
            Clipboard.SetText(lblKey.Text);
        }

        private void listViewContextMenuStrip_Opening(object sender, System.ComponentModel.CancelEventArgs e)
        {

        }

        private void toolStripMenuItem2_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < listView.Items.Count; i++)
            {
                if (listView.Items[i].Selected)
                {
                    String txt = listView.Items[i].ToString();
                    String bs = "ListViewItem: {";
                    txt = txt.Remove(txt.Length - 1);
                    txt = txt.TrimStart(bs.ToCharArray());
                    Clipboard.SetText(txt);
                    break;
                }
            }  
        }

        private void copyUninstallStringToolStripMenuItem_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < listView.Items.Count; i++)
            {
                if (listView.Items[i].Selected)
                {
                    String txt = listView.Items[i].SubItems[1].ToString();
                    String bs = "ListViewSubItem: {";
                    txt = txt.Remove(txt.Length - 1);
                    txt = txt.TrimStart(bs.ToCharArray());
                    Clipboard.SetText(txt);
                    break;
                }
            } 
        }
    }
}
